=pod
 * Call.pm
 * 
 * Copyright (C) 2012 Elena Shishkina
 * 
 * This file is part of ServerInventory.
 * 
 * ServerInventory is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ServerInventory is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ServerInventory.  If not, see <http://www.gnu.org/licenses/>.
=cut

use utf8;
use 5.12.0;
package ServerInventory::Call;
use Mojo::Base 'Mojolicious::Controller';
use ServerInventory;
use ServerInventory::DataCenter;
use ServerInventory::Rack;
use ServerInventory::Export;
use ServerInventory::Import;
use ServerInventory::Vendor;
use ServerInventory::Server;
use DBI;

sub static {
    my $self = shift;
    say $ServerInventory::conf->{db}->{host};
    $self->render_static('index.html');
}

sub install {
    my $self = shift;
    my $conf = $ServerInventory::conf;
    my $dbh = $self->connect();
    if (ref $dbh) {
        my $res;
        given ($conf->{db}->{driver}) {
            when ('sqlite') {
                $res = $self->install_sqlite($dbh);
            }
            when ('postgresql') {
                $res = $self->install_pg($dbh);
            }
            when ('mysql') {
                $res = $self->install_mysql($dbh);
            }
            default {
                $res = 'ERROR_UNKNOWN_DRIVER';
            }
        }
        $self->stash('result' => $res);
    } else {
        $self->stash('result' => $dbh);
    }
    $self->render(template => 'install');
}

sub call {
    my $self = shift;
    $self->{conf} = $ServerInventory::conf;
    $self->{dbh} = $self->connect;
    #$self->render(text => 'Not imlemented');
    given ($self->param('source')) {
        when ('dc') {
            ServerInventory::DataCenter->new->Process($self);
        }
        when ('rack') {
            ServerInventory::Rack->new->Process($self);
        }
        when ('server') {
            ServerInventory::Server->new->Process($self);
        }
        when ('export') {
            ServerInventory::Export->new->Process($self);
        }
        default {
            $self->render(status => 400, text => 'Bad request');
        }
    }
}

sub import {
    my $self = shift;
    $self->{conf} = $ServerInventory::conf;
    $self->{dbh} = $self->connect;
    $self->render(text => ServerInventory::Import->new->Process($self));
}

sub install_sqlite {
    my $self = shift;
    my $dbh = shift;
    my $res = "Creating table dcs... ";
    if ($dbh->do('create table dcs (id integer primary key, name text, address text, power integer, notes text, width integer, height integer, entrance_left integer, entrance_top integer)')) {
        $res .= "OK\n";
    } else {
        $res .= "ERROR: " . $dbh->errstr;
        return $res;
    }
    $res .= 'Creating table vendors... ';
    if ($dbh->do('create table vendors (id integer primary key, name text unique, color text)')) {
        $res .= "OK\n";
    } else {
        $res .= 'ERROR: ' . $dbh->errstr;
        return $res;
    }
    $res .= 'Creating table racks... ';
    if ($dbh->do('create table racks (id integer primary key, name text, units integer, notes text, dc_id integer references dcs(id), vendor_id integer references dcs(id), top integer, left integer, responsible text)')) {
        $res .= "OK\n";
    } else {
        $res .= 'ERROR: ' . $dbh->errstr;
        return $res;
    }
    $res .= 'Creating table inventory... ';
    if ($dbh->do('create table inventory (id integer primary key, name text, model text, type text, sn text, [in] text, pusage integer, pstate integer, units text, mgmtip text, notes text, color text, rack_id integer references racks(id), vendor_id integer references vendors (id), responsible text)')) {
        $res .= "OK\n";
    } else {
        $res .= 'ERROR: ' . $dbh->errstr;
        return $res;
    }
    $res .= 'Creating table reserved_units... ';
    if ($dbh->do('create table reserved_units (rack_id integer references racks(id), unit integer, primary key (rack_id, unit))')) {
        $res .= "OK\n";
    } else {
        $res .= 'ERROR: ' . $dbh->errstr;
        return $res;
    }
    $res .= 'Creating table server_ips... ';
    if ($dbh->do('create table server_ips (server_id integer references inventory(id), ip text, primary key (server_id, ip))')) {
        $res .= "OK\n";
    } else {
        $res .= 'ERROR: ' . $dbh->errstr;
        return $res;
    }
}

sub install_pg {
    my $self = shift;
    my $dbh = shift;
    return 'ERROR_NOT_IMPLEMENTED';
}

sub install_mysql {
    my $self = shift;
    my $dbh = shift;
    return 'ERROR_NOT_IMPLEMENTED';
}

sub connect {
    my $self = shift;
    my $conf = $ServerInventory::conf;
    given ($conf->{db}->{driver}) {
        when ('mysql') {
            return 'ERROR_EMPTY_NAME' unless $conf->{db}->{name};
            return 'ERROR_EMPTY_HOST' unless $conf->{db}->{host};
            return 'ERROR_EMPTY_USER' unless $conf->{db}->{user};
            return 'ERROR_EMPTY_PORT' unless $conf->{db}->{port};
            return 'ERROR_EMPTY_PASSWORD' unless $conf->{db}->{password};
            return DBI->connect('DBI:mysql:database=' . $conf->{db}->{name} . ';host=' . $conf->{db}->{host} . ';port=' . $conf->{db}->{port}, $conf->{db}->{user}, $conf->{db}->{password}, {RaiseError => 0, PrintError => 1}) || 'ERROR_DB_CONNECT: ' . $DBI::errstr;
        }
        when ('postgresql') {
            return 'ERROR_EMPTY_NAME' unless $conf->{db}->{name};
            return 'ERROR_EMPTY_HOST' unless $conf->{db}->{host};
            return 'ERROR_EMPTY_USER' unless $conf->{db}->{user};
            return 'ERROR_EMPTY_PORT' unless $conf->{db}->{port};
            return 'ERROR_EMPTY_PASSWORD' unless $conf->{db}->{password};
            return DBI->connect('dbi:Pg:dbname=' . $conf->{db}->{name} . ';host=' . $conf->{db}->{host} . ';port=' . $conf->{db}->{port}, $conf->{db}->{user}, $conf->{db}->{password}, {RaiseError => 0, PrintError => 1}) || 'ERROR_DB_CONNECT: ' . $DBI::errstr;
        }
        when ('sqlite') {
            return 'ERROR_EMPTY_NAME' unless $conf->{db}->{name};
            return DBI->connect('dbi:SQLite:dbname='  . $conf->{db}->{name}, '', '', {RaiseError => 0, PrintError => 1}) || 'ERROR_DB_CONNECT: ' . $DBI::errstr;
        }
        default {
            return 'ERROR_UNKNOWN_DRIVER';
        }
    }
}

1;

